package com.kaili.basic.controller;

import com.kaili.basic.mapper.BaseMenuMapper;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.pojo.entity.LoginDetail;
import com.kaili.common.pojo.entity.MdmSysBaseMenu;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.TreeParserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "菜单管理", description = "菜单管理")
public class MdmMenuController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BaseMenuMapper baseMenuMapper;


    @GetMapping("/user/navlist")
    @ApiOperation(value = "查询菜单", notes = "查询菜单")
    public List<MdmSysBaseMenu> queryPageList(){
        Integer integer = LoginDetailUtils.getCurrent().getId();
        System.out.println(integer);
        List<MdmSysBaseMenu> list = baseMenuMapper.getCurrentMenu(LoginDetailUtils.getCurrent().getId());
        List<MdmSysBaseMenu> treeList = TreeParserUtil.getTreeList("0", list);
        //格式化菜单树
        for (MdmSysBaseMenu menuAuthVo:treeList) {
            TreeParserUtil.menuFormat(menuAuthVo.getChild(),menuAuthVo);
        }
        return treeList;
    }

    @GetMapping(value = "/getToken")
    @ApiOperation("获取当前用户Token")
    public String queryCurrentToken(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        System.out.println(header);
        LoginDetail userDetails= null;
        try {
            userDetails = (LoginDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return userDetails.getToken();
        } catch (Exception e) {
            logger.debug("当前用户尚为登陆："+e);
            return UUID.randomUUID().toString();
        }
    }

}


